<template>
  <div class="app-container">
    <el-row :gutter="0">
      <!--方案数据-->
      <el-form class="formBox searchForm" :model="queryParams" ref="queryForm" size="small" :inline="true"
               v-show="showSearch"
               label-width="68px">
        <el-form-item label="医院" prop="hospital" clearable>
          <department :isHospital="true" :isdepat="true" @refreshData="refreshData" deptWidth="200px" ref="department">
          </department>
        </el-form-item>
        <el-form-item label="适用专病" prop="diseaseId" clearable>
          <el-select v-model="queryParams.diseaseId" placeholder="请选择专病名称" clearable filterable style="width: 200px">
            <el-option v-for="item in classifyList" :key="item.diseaseClassifyId" :label="item.diseaseClassifyName"
                       :value="item.diseaseClassifyId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目类别" prop="planCategoryId" clearable>
          <el-select v-model="queryParams.planCategoryId" placeholder="请选择方案大类" clearable filterable
                     style="width: 200px">
            <el-option v-for="dict in dict.type.sys_plan_category" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方案名称" prop="medicalEquipment">
          <el-input v-model="queryParams.medicalEquipment" placeholder="请输入方案名称" clearable style="width:200px"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="状态" prop="status" clearable>
          <el-select v-model="queryParams.status" placeholder="状态" clearable filterable style="width:200px">
            <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="formBox searchForm" ref="statisticalTable" style="margin-bottom:0px!important;">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                       v-hasPermi="['plan:single:add']">新增
            </el-button>
            <el-button type="success" plain icon="el-icon-plus" size="mini" @click="platformTempDialogVisible = true"
                       v-hasPermi="['plan:single:temp']">引入平台模板
            </el-button>
            <!-- <el-button type="success" plain icon="el-icon-plus" size="mini" @click="AddplatformTemp(2)"
              :disabled="multiple" v-if="isAdmin">加入平台模板</el-button> -->
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange" border
                  :height="statisticalTableH" ref="table">
          <el-table-column type="selection" width="55" v-if="isAdmin">
          </el-table-column>
          <el-table-column sortable label="方案编号" align="center" prop="planId" width="100"/>
          <el-table-column label="医院" align="center" prop="hospitalName" width="150">
            <template slot-scope="scope">
              <departTag :options="deptNameLists" :value="scope.row.hospitalId"/>
            </template>
          </el-table-column>
          <el-table-column label="科室" align="center" prop="deptName" width="150">
            <template slot-scope="scope">
              <departTag :options="deptNameLists" :value="scope.row.deptId"/>
            </template>
          </el-table-column>
          <el-table-column sortable label="适用专病" align="center" prop="diseaseName" :show-overflow-tooltip="true"
                           width="100"/>
          <el-table-column sortable label="项目类别" align="center" prop="planCategoryId" :show-overflow-tooltip="true"
                           width="100">
            <template slot-scope="scope">
              <span>{{ matchDict("sys_plan_category", scope.row.planCategoryId) }}</span>
            </template>
          </el-table-column>

          <el-table-column sortable label="方案名称" align="center" prop="medicalEquipment" :show-overflow-tooltip="true"
                           width="120"/>
          <el-table-column sortable label="用法" align="center" prop="usageId" :show-overflow-tooltip="true" width="100">
            <template slot-scope="scope">
              <span>{{ matchDict("sys_plan_usage", scope.row.usageId) }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="用量及单位" align="center" prop="dosage" :show-overflow-tooltip="true"
                           width="120">
            <template slot-scope="scope">
              <span>{{
                  scope.row.dosage ? scope.row.dosage + matchDict("sys_dosage_unit", scope.row.dosageUnit) : ''
                }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="频次" align="center" prop="frequencyId" :show-overflow-tooltip="true"
                           width="100">
            <template slot-scope="scope">
              <span>{{ matchDict("sys_dosage_frequency", scope.row.frequencyId) }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="默认数量" align="center" prop="num" :show-overflow-tooltip="true" width="100"/>
          <el-table-column sortable label="单价" align="center" prop="price" :show-overflow-tooltip="true" width="100"/>
          <!-- <el-table-column sortable label="优惠率" align="center" prop="discount" :show-overflow-tooltip="true"
            width="100" /> -->
          <el-table-column sortable label="先敷药" align="center" prop="isApplication" :show-overflow-tooltip="true"
                           width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.isApplication == 0 ? '是' : (scope.row.isApplication == 1 ? '否' : '') }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="治疗仪器" align="center" prop="instruments" :show-overflow-tooltip="true"
                           width="160"/>
          <el-table-column sortable label="治疗参数" align="center" prop="parameters" :show-overflow-tooltip="true"
                           width="160"/>
          <el-table-column sortable label="领取地点" align="center" prop="pickingLocation" width="160">
            <template slot-scope="scope">
              <span>{{
                  matchDict("sys_picking_location", scope.row.pickingLocation)
                }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
          <el-table-column sortable label="状态" align="center" width="100">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
                         @change="handleStatusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column sortable label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['plan:single:edit']">修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['plan:single:remove']">删除
              </el-button>
              <!-- <el-button type="text" icon="el-icon-plus" size="mini" @click="AddplatformTemp(2, scope.row)"
                v-if="isAdmin">加入平台模板</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>
      </div>
    </el-row>

    <!-- 添加或修改单项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="670px" append-to-body :close-on-click-modal="false"
               @close="closeMedicalEquipmentDialog" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="80px" label-position="left">
        <el-form-item label="方案名称" prop="medicalEquipment">
          <el-input v-model="form.medicalEquipment" placeholder="请输入方案名称" style="width: 540px"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="24">
            <el-form-item label="使用科室" prop="deptId" v-if="open">
              <department :isAdd="true" @changedepatData="changedepatData" :HospitalId="form.hospitalId"
                          :Optiohospitals="true"
                          :deptIds="form.deptId" :isdisabled="false" deptWidth="202px"></department>
            </el-form-item>
            <el-form-item label="是否用于色斑平台" label-width="140px">
              <el-radio-group v-model="form.platformType">
                <el-radio v-for="dict in stainArr" :key="dict.value" :label="dict.value">{{
                    dict.label
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>

        <el-form-item label="适用专病" prop="diseaseIds">
          <el-select v-model="form.diseaseIds" multiple filterable clearable placeholder="请选择适用专病" style="width: 540px">
            <el-option v-for="item in classifyList" :key="item.diseaseClassifyId" :label="item.diseaseClassifyName"
                       :value="item.diseaseClassifyId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目类别" prop="planCategoryId">
          <el-select v-model="form.planCategoryId" clearable filterable placeholder="请选择项目类别" style="width: 202px">
            <el-option v-for="dict in dict.type.sys_plan_category" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用法" prop="usageId" style="margin-left: 45px">
          <el-select v-model="form.usageId" clearable filterable placeholder="请选择用法" style="width: 202px">
            <el-option v-for="dict in dict.type.sys_plan_usage" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用量" prop="dosage">
          <el-input v-model="form.dosage" placeholder="请输入用量" style="width: 202px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="用量单位" prop="dosageUnit" style="margin-left: 45px">
          <el-select v-model="form.dosageUnit" clearable filterable placeholder="请选择用量单位" style="width: 202px">
            <el-option v-for="dict in dict.type.sys_dosage_unit" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="单价" prop="price">
          <el-input-number v-model="form.price" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="领取地点" style="margin-left: 45px">
          <el-select v-model="form.pickingLocation" clearable filterable placeholder="领取地点" style="width: 202px;">
            <el-option v-for="dict in dict.type.sys_picking_location" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item label="频次" prop="frequencyId" label-width="60px">
            <el-select v-model="form.frequencyId" clearable filterable placeholder="请选择频次" style="width: 202px">
              <el-option v-for="dict in dict.type.sys_dosage_frequency" :key="dict.value" :label="dict.label"
                         :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="默认数量" prop="num" style="margin-left: 10px">
            <el-input v-model.number="form.num" :min="0" type="number" @keydown.native="onlyWriteNum"
                      style="width:70px"></el-input>
          </el-form-item>
          <el-form-item label="治疗周期(天)" prop="treatmentCycle" label-width="100px" style="margin-left: 5px">
            <el-input v-model.number="form.treatmentCycle" :min="0" type="number" style="width:70px"
                      @keydown.native="onlyWriteNum"></el-input>
          </el-form-item>
        </el-row>

        <el-form-item label="药品规格" prop="drugSpecificationsNum">
          <el-input v-model.number="form.drugSpecificationsNum" placeholder="数量" :min="0" type="number" @keydown.native="onlyWriteNum"
                    style="width:80px"></el-input>
        </el-form-item>
        <el-form-item label="" prop="drugSpecificationsUnit">
          <el-input v-model="form.drugSpecificationsUnit" placeholder="单位" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="包装规格" prop="packagingSpecificationNum">
          <el-input v-model.number="form.packagingSpecificationNum" placeholder="数量" :min="0" type="number" @keydown.native="onlyWriteNum"
                    style="width:80px"></el-input>
        </el-form-item>
        <el-form-item label="" prop="packagingSpecificationUnit">
          <el-input v-model="form.packagingSpecificationUnit" placeholder="单位" style="width: 120px"></el-input>
        </el-form-item>

        <!-- <el-form-item label="优惠率" prop="discount" style="margin-left: 45px">
          <el-input-number v-model="form.discount" :precision="2" :step="0.01" :min="0" :max="1"></el-input-number>
        </el-form-item> -->
        <el-form-item label="治疗仪器" prop="instruments">
          <el-select v-model="form.instruments" multiple filterable clearable placeholder="请选择常用治疗仪器名称"
                     style="width: 540px">
            <el-option v-for="item in instrumentsList" :key="item.deviceId" :label="item.deviceName"
                       :value="item.deviceId"></el-option>
          </el-select>
        </el-form-item>


        <div v-show="form.planCategoryId == 2">
          <el-form-item label="治疗参数">
            <el-select v-model="form.fakeParameters" clearable filterable placeholder="请选择治疗参数名称" style="width: 170px"
                       @change="changeFakeParameters">
              <el-option v-for="dict in dict.type.sys_treatment_parameter" :key="dict.value" :label="dict.label"
                         :value="dict.value"></el-option>
            </el-select>
            <!--                <el-input v-model="form.parameters" placeholder="请输入治疗参数" style="width: 540px"></el-input>-->
          </el-form-item>
          <el-form-item label="常用值">
            <el-input v-model="form.fakeValue" clearable placeholder="请输入常用参数值" style="width: 202px"
                      @input="fakechange"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 65px" @click="addParameters"> 添加</el-button>
          </el-form-item>
          <el-table :data="parametersList" border>
            <el-table-column sortable label="治疗参数" align="center" prop="label"/>
            <el-table-column sortable label="常用值" align="center" prop="value"/>
            <el-table-column label="操作" align="center" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-delete"
                           @click="deleteParameters(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-form-item label="先敷药">
            <el-radio-group v-model="form.isApplication">
              <el-radio v-for="dict in yesOrNoArr" :key="dict.value" :label="dict.value">{{
                  dict.label
                }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态" style="margin-left: 145px">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{
                  dict.label
                }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </div>

        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" style="width: 540px"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 引入平台模板 -->
    <el-dialog :title="platformTitle" :visible.sync="platformTempDialogVisible" v-dialogDrag width="90%"
               :close-on-click-modal="false">
      <platformTemp v-if="platformTempDialogVisible" @closeplatformTempDialogVisible="closeplatformTempDialogVisible"
                    type="2" @refresh="refreshFormList">
      </platformTemp>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPlan,
  getPlan,
  delPlan,
  addPlan,
  updatePlan,
  changePlanStatus,
} from "@/api/system/plan";
import department from "@/views/components/department/index.vue";
import departTag from "@/views/components/department/departTag.vue";
import platformTemp from "@/views/components/platformTemp/planTemp.vue";
import {deptNameList} from "@/api/system/dept";
import {getToken} from "@/utils/auth";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listClassify} from "@/api/system/classify";
import {listDevice} from "@/api/system/device";

export default {
  name: "single",
  dicts: [
    "sys_normal_disable",
    "sys_manage_diseases",
    "sys_plan_category",
    "sys_dosage_unit",
    "sys_treatment_parameter",
    "sys_dosage_frequency",
    "sys_plan_usage",
    "sys_yes_no",
    "sys_picking_location",
  ],
  components: {department, departTag, platformTemp},
  data() {
    return {
      statisticalTableH: undefined,
      platformTempDialogVisible: false,
      platformTitle: '平台模板-单项',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 方案表格数据
      planList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {
        createBy: undefined,
        createTime: undefined,
        delFlag: undefined,
        diseaseId: undefined,
        diseaseIds: [],
        dosage: undefined,
        dosageUnit: undefined,
        frequencyId: undefined,
        medicalEquipment: undefined,
        num: undefined,
        price: undefined,
        patientId: undefined,
        patientPlanId: undefined,
        planCategoryId: undefined,
        pickingLocation: null,
        remark: undefined,
        restTreatmentNum: undefined,
        searchValue: undefined,
        treatmentNum: undefined,
        updateBy: undefined,
        updateTime: undefined,
        usageId: undefined,
        visitId: undefined,
        isApplication: 1,
        status: '1',
        instruments: undefined,
        fakeParameters: undefined,
        fakeValue: '',
        hospitalId: undefined,
        deptId: undefined,
        treatmentCycle: undefined,
        platformType: 0,
        drugSpecificationsNum: undefined,
        drugSpecificationsUnit: undefined,
        packagingSpecificationNum: undefined,
        packagingSpecificationUnit: undefined,

      },
      parametersList: [],
      deptNameLists: [],
      // 专病管理列表
      classifyList: [],
      //治理仪器列表
      instrumentsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        diseaseId: undefined,
        planCategoryId: undefined,
        medicalEquipment: undefined,
        status: undefined,
        hospitalId: undefined,
        deptId: undefined,
      },
      // 套餐有关参数
      // mealTitle:"",
      // openMeal:false,
      // mealForm:{
      //   type: [],
      //   diseaseIds: [],
      //   createBy: undefined,
      //   createTime: undefined,
      //   delFlag: undefined,
      //   remark: undefined,
      //   searchValue: undefined,
      //   setMealId: undefined,
      //   setMealName: undefined,
      //   status: undefined,
      //   updateBy: undefined,
      //   updateTime: undefined
      // },

      // 表单校验
      rules: {
        diseaseIds: [
          {
            required: true,
            message: "专病名称不能为空",
            trigger: "blur",
          },
        ],
        planCategoryId: [
          {
            required: true,
            message: "方案类别不能为空",
            trigger: "blur",
          },
        ],
        medicalEquipment: [
          {
            required: true,
            message: "方案名称不能为空",
            trigger: "blur",
          },
          {
            min: 2,
            max: 20,
            message: "方案名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        usageId: [
          {
            required: true,
            message: "用法不能为空",
            trigger: "blur",
          },
        ],
        deptId: [
          {
            required: true,
            message: "科室必选",
            trigger: "blur",
          },
        ],
        dosage: [
          {
            required: true,
            message: "用量不能为空",
            trigger: "blur",
          },
        ],
        // dosageUnit: [
        //   {
        //     required: true,
        //     message: "用量单位不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // frequencyId: [
        //   {
        //     required: true,
        //     message: "频次不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // num: [
        //   {
        //     required: true,
        //     message: "数量不能为空",
        //     trigger: "blur",
        //   },
        // ],
      },
      //是否数组
      yesOrNoArr: [{value: 0, label: '是'}, {value: 1, label: '否'},],
      stainArr: [{value: 0, label: '否'}, {value: 1, label: '是'},],
    };
  },
  watch: {
    showSearch() {
      this.$nextTick(() => {
        this.changeHeight()
      }, 1000)

    }
  },
  created() {
    // this.getList();
    this.getClassifyList()
    this.getInstrumentsList()
  },
  mounted() {
    this.changeHeight()
  },
  computed: {
    isAdmin() {
      return this.$store.state.user.name === 'admin' ? true : false
    }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    changeHeight() {
      if (this.$refs.statisticalTable) {
        const statisticalTableObj = this.$refs.statisticalTable.getBoundingClientRect();
        let h = statisticalTableObj.top + 100
        this.statisticalTableH = 'calc(100vh - ' + h + 'px)'
      } else {
        this.statisticalTableH = '500px'
      }
    },
    AddplatformTemp() {//加入平台模板

    },
    /** 平台模板引用或者批量引用点击后关闭弹窗并刷新本院表单列表 */
    refreshFormList() {
      this.platformTempDialogVisible = false
      this.getList()
    },
    closeplatformTempDialogVisible() {
      this.platformTempDialogVisible = false
    },
    onlyWriteNum(e) {
      e.returnValue = (['e', 'E', '-', '+', '.'].includes(e.key)) ? false : e.returnValue
      return e
    },
    closeMedicalEquipmentDialog() {
      this.parametersList = []
    },
    refreshData(data) {
      this.queryParams.pageNum = 1;
      this.queryParams.hospitalId = data.hospitalId
      this.queryParams.deptId = data.deptId
      this.getdeptNameList();
    },
    changedepatData(data) {
      console.log(data)
      if (data && data.length > 0) {
        this.form.deptId = data[0].deptId
        this.form.hospitalId = data[0].parentId == 100 ? data[0].deptId : data[0].parentId
      } else {
        this.form.deptId = undefined
        this.form.hospitalId = undefined
      }
    },
    // 回显医院及门诊
    getdeptNameList() {
      deptNameList().then((response) => {
        this.deptNameLists = response.data;
        this.getList();
      });
    },
    changeFakeParameters() {
      let that = this
      that.form.fakeValue = ''
    },
    fakechange(e) {//input 刷新
      this.$forceUpdate()
    },
    addParameters() {
      const mdata = this.parametersList.filter(item => {
        return this.matchDict('sys_treatment_parameter', this.form.fakeParameters) == item.label
      })
      if (mdata.length > 0) {
        this.$message.info('已经有此参数，请勿重复添加！')
      } else {
        this.parametersList.push({
          label: this.matchDict('sys_treatment_parameter', this.form.fakeParameters),
          value: this.form.fakeValue
        })
      }
    },
    deleteParameters(row) {
      this.parametersList = this.parametersList.filter(item => {
        return item.label != row.label
      })
    },
    /** id转化成对应的文字 */
    matchDict: function (key, id) {
      let _this = this;
      const result = _this.dict.type[key];
      let textValue = "";
      result.forEach((item) => {
        if (item.raw.dictValue == id) {
          textValue = item.raw.dictLabel;
        }
      });
      return textValue;
    },
    /** 查询方案列表 */
    getList() {
      this.loading = true;
      listPlan(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.planList = response.rows;
          this.planList.forEach(item => {
            if (item.parameters) {
              item.parameters = this.getParametersValue(item.parameters)
            }
          })
          this.total = response.total;
          this.loading = false;

        }
      );
    },
    getParametersValue(row) {
      // console.log(row)
      let text = ''
      JSON.parse(row).forEach((item, index) => {
        text += item.label + '(' + item.value + ');'
      })
      return text
    },
    /** 查询专病列表 */
    getClassifyList() {
      listClassify(
        {
          pageNum: 1,
          pageSize: 9999,
          status: '0',
        }
      ).then((response) => {
        this.classifyList = response.rows;

      });
    },
    //查询设备列表 (治疗仪器列表)
    getInstrumentsList() {
      listDevice({
        pageNum: 1,
        pageSize: 9999,
        deviceName: undefined,
        status: '0',
        deviceType: '0',
      }).then((response) => {
        this.instrumentsList = response.rows;
      });
    },


    // 方案状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm(
          "确认要" +
          text +
          "方案名称为'" +
          row.medicalEquipment +
          "'方案吗？"
        )
        .then(function () {
          return changePlanStatus(row.planId, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        num: 1,
        dosage: 1,
        price: 0,
        status: "0",
        remark: undefined,
        diseaseId: undefined,
        diseaseIds: [],
        isApplication: 1,
        fakeParameters: undefined,
        fakeValue: '',
        treatmentCycle: undefined,
        hospitalId: undefined,
        deptId: undefined,
        platformType: 0,
        drugSpecificationsNum: undefined,
        drugSpecificationsUnit: undefined,
        packagingSpecificationNum: undefined,
        packagingSpecificationUnit: undefined,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      if (this.$refs.department) {
        this.$refs.department.reset()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.planId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增单项按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增方案单项";
      this.getClassifyList()
      this.getInstrumentsList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getClassifyList()
      this.getInstrumentsList()
      const planId = row.planId || this.ids;
      getPlan(planId).then((response) => {
        console.log('..getPlan..', response)
        let data = response.data;
        this.form = response.data;
        if (this.form.isApplication == null) {
          this.form.isApplication = 1
        }
        if (this.form.instruments) {
          this.form.instruments = this.form.instruments.split(",").map(Number)
        }
        // this.form.instruments=['14',15,16,17]
        if (this.form.parameters) {
          this.parametersList = JSON.parse(this.form.parameters)
        }
        if (data.platformType == undefined || data.platformType == null || data.platformType == '') {
          this.form.platformType = 0
        }
        this.open = true;
        this.title = "修改方案单项";
      });

    },

    /** 提交按钮 */
    submitForm: function () {
      console.log('submitForm', this.form)

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.planId != undefined) {
            if (Array.isArray(this.form.instruments)) {
              this.form.instruments = this.form.instruments.join(",")
            }
            // if (Array.isArray(this.form.parameters)) {
            //   this.form.parameters = this.form.parameters.join(",")
            // }
            this.form.parameters = JSON.stringify(this.parametersList)
            updatePlan(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }

            });
          } else {

            if (Array.isArray(this.form.instruments)) {
              this.form.instruments = this.form.instruments.join(",")
            }
            // if (Array.isArray(this.form.parameters)) {
            //   this.form.parameters = this.form.parameters.join(",")
            // }
            this.form.parameters = JSON.stringify(this.parametersList)
            console.log('this.form', this.form)
            addPlan(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.form = {};
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal
        .confirm('是否确认删除方案编号为"' + planIds + '"的数据项？')
        .then(function () {
          return delPlan(planIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },

  },
};
</script>
<style scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

::v-deep input[type="number"] {
  -moz-appearance: textfield;
}
.obliqueLine{
  font-size: 30px;
}
</style>
